//
//  AddViewController.m
//  Clock
//
//  Created by 郭明皓 on 16/6/23.
//  Copyright © 2016年 郭明皓. All rights reserved.
//

#import "AddViewController.h"
#import "RepeadViewController.h"
#import "TitleViewController.h"
@interface AddViewController ()
@property (nonatomic, strong)UITableView *addTableview;
@property (nonatomic, strong)NSArray *tableArr;
@property (nonatomic, strong)NSMutableArray *rightArr;
@end

@implementation AddViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        arrayHour = @[@"00", @"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23"];
        self.tableArr = @[@"重复", @"标签", @"铃声", @"稍后提醒"];
        self.rightArr = [NSMutableArray arrayWithObjects:@"从不", @"闹钟", @"雷达", @"", nil];

        arrayMin = [NSMutableArray array];
        for (int i = 0; i < 60; i++) {
            NSString *tempString = [NSString stringWithFormat:@"%02d", i];
            [arrayMin addObject:tempString];
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    UINavigationController *navc = [[UINavigationController alloc]init];
    

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"存储" style:UIBarButtonItemStyleDone target:self action:@selector(store:)];
    
    //定义pickerView
    timePickerView = [[UIPickerView alloc]init];
    timePickerView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 320);
    timePickerView.backgroundColor = [UIColor whiteColor];
    timePickerView.delegate = self;
    timePickerView.showsSelectionIndicator = YES;
    
    [timePickerView selectRow:0 inComponent:0 animated:YES];
    [timePickerView selectRow:0 inComponent:1 animated:YES];
    [self.view addSubview:timePickerView];
    
    self.addTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 320, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 320) style:UITableViewStyleGrouped];
    [self.view addSubview:_addTableview];
    self.addTableview.delegate = self;
    self.addTableview.dataSource = self;
    
    
//    UILocalNotification *notification=[[UILocalNotification alloc] init];
//    if (notification!=nil) {
//        NSDate *now=[NSDate new];
//        notification.fireDate=[now dateByAddingTimeInterval:10];//10秒后通知
//        notification.repeatInterval=0;//循环次数，kCFCalendarUnitWeekday一周一次
//        notification.timeZone=[NSTimeZone defaultTimeZone];
//        notification.applicationIconBadgeNumber=1; //应用的红色数字
//        notification.soundName= UILocalNotificationDefaultSoundName;//声音，可以换成alarm.soundName = @"myMusic.caf"
//        //去掉下面2行就不会弹出提示框
//        notification.alertBody=@"通知内容";//提示信息 弹出提示框
//        notification.alertAction = @"打开";  //提示框按钮
//        //notification.hasAction = NO; //是否显示额外的按钮，为no时alertAction消失
//        //NSDictionary*infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
//        //notification.userInfo = infoDict; //添加额外的信息
//        
//        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
//    }

    
}
//-(UILocalNotification *)notificationWithTime:(NSDate *)time {
//    NSMutableArray *musicArray = [model musiclistadd];
//    AlarmCount *shareCount = [AlarmCount shareCount];
//    NSDate *date = [self.datePicker date];
//    UILocalNotification *notifity=[[UILocalNotification alloc] init];
//    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
//    NSString *timer = [formatter stringFromDate:time];
//    NSLog(@"%@",timer);
//    notifity.fireDate= [formatter dateFromString:timer];
//    notifity.timeZone=[NSTimeZone defaultTimeZone];
//    notifity.repeatInterval=  kCFCalendarUnitWeek ;
//    notifity.alertBody=@"时间到了";
//    notifity.soundName= musicArray[shareCount.bCount];
//    notifity.alertAction = @"打开";
//    NSString *key = [formatter stringFromDate:date];
//    NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"111" forKey:key];
//    [[UIApplication sharedApplication] scheduleLocalNotification:notifity];
//    notifity.userInfo = userDict;
//    return notifity;
//}
#pragma mark uipicviewDatasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    //两列
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    int i = 0;
    if (component == 0) {
        i = 24;
    }
    if (component == 1) {
        i = 60;
    }
    return i;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *textLabel = [[UILabel alloc]init];
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.frame = CGRectMake(0, 0, 45, 45);
    textLabel.textAlignment = NSTextAlignmentRight;
    
    if (component == 0) {
        textLabel.text = [arrayHour objectAtIndex:(row % 24)];
    }
    if (component == 1) {
        textLabel.text = [arrayMin objectAtIndex:(row % 60)];
    }
    return textLabel;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        hour = [[arrayHour objectAtIndex:row % 24]intValue];
    }
    if (component == 1) {
        min = [[arrayMin objectAtIndex:row % 60]intValue];
    }
}

#pragma mark tableviewData
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *addtableview = @"tableviewId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:addtableview];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:addtableview];
    }
    cell.textLabel.text = self.tableArr[indexPath.row];
    cell.detailTextLabel.text = self.rightArr[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        RepeadViewController *repead = [[RepeadViewController alloc]init];
        [self.navigationController pushViewController:repead animated:YES];
    }
    if (indexPath.row == 1) {
        TitleViewController *title = [[TitleViewController alloc]init];
        [self.navigationController pushViewController:title animated:YES];
        title.title = self.rightArr[indexPath.row];
        NSLog(@"点击");
        title.block = ^(NSString *str) {
            [self.rightArr replaceObjectAtIndex:1 withObject:str];
            NSLog(@"%@", self.rightArr[1]);
            [self.addTableview reloadData];
            return str;
        };
        
    }
}

- (void)back:(UIBarButtonItem *)back {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)store:(UIBarButtonItem *)store {
    NSInteger HourRow=[timePickerView selectedRowInComponent:0];
    NSInteger MinRow=[timePickerView selectedRowInComponent:1];
    //        然后是获取这个行中的值，就是数组中的值
    NSString *hourStr = [arrayHour objectAtIndex:HourRow];
    NSString *minstr = [arrayMin objectAtIndex:MinRow];
    self.times(hourStr, minstr);
    
    [self.navigationController popToRootViewControllerAnimated:YES];
//    [self dismissViewControllerAnimated:YES completion:^{
//        
//    }];
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
